import { ApiCall } from "tsrpc";
import { ReqChopping, ResChopping } from "../../../shared/master/word/PtlChopping";
import { player } from "../../../model/fight/body/player";
import { Tpl_gifts_default, template } from "../../../shared/master/MsgAction";
import app from "../../../app";
import manage_user from "../../model/manage/manage_user";
import god from "../../../god";
import { logger } from "../../../model/server/logger";
import { rank_type, taskKey } from "../../../shared/interface";
import manage_task from "../../model/manage/manage_task";
let timer;
let choppingMap: Map<string, { pos: { x: number, y: number, name: string }, pgs: number, start_pos: { x: number, y: number } }> = new Map()
export default async function (call: ApiCall<ReqChopping, ResChopping>) {
    call.succ({})
    let user = call.req._player as player;
    let _cell = user.getPosition();
    let _word = _cell.getWord();
    let cell_top = _word.getCell(_cell.x, _cell.y + 1)
    let cell_down = _word.getCell(_cell.x, _cell.y - 1)
    let cell_right = _word.getCell(_cell.x + 1, _cell.y)
    let cell_left = _word.getCell(_cell.x - 1, _cell.y)


    if (choppingMap.has(user.uuid)) {
        user.sendMsg(template.default_none, '🪓正在砍树中...')
        return;
    }
    if (cell_top && cell_down && cell_right && cell_left) {
        return;
    }
    if(!_word.cfg.chopping){
        user.sendMsg(template.default_none, '🪓此处禁止砍树')
        return;
    }
    let wallGroup: {
        x: number,
        y: number,
        name: string
    }[] = [];
    if (!cell_top) {
        wallGroup.push({ x: _cell.x, y: _cell.y + 1, name: '上方' })
    }
    if (!cell_down) {
        wallGroup.push({ x: _cell.x, y: _cell.y - 1, name: '下方' })
    }
    if (!cell_right) {
        wallGroup.push({ x: _cell.x + 1, y: _cell.y, name: '右方' })
    }
    if (!cell_left) {
        wallGroup.push({ x: _cell.x - 1, y: _cell.y, name: '左方' })
    }
    app.shuffleArray(wallGroup)
    let pos = {
        x: wallGroup[0].x,
        y: wallGroup[0].y,
        name: wallGroup[0].name
    }
    choppingMap.set(user.uuid, {
        pos: pos,
        start_pos: { x: _cell.x, y: _cell.y },
        pgs: app.random(10, 20)
    })
    user.sendMsg(template.default_none, `🪓伐木等级${user.leve_chopping.num}(${app.BN(user.leve_chopping.now)}/${app.BN(user.leve_chopping.max)})\n开始拿起斧头扎起袖子准备砍树...`)
    if (timer) {
        clearInterval(timer)
    }
    timer = setInterval(() => {
        choppingMap.forEach(async (element, uuid) => {
            let _user = manage_user.locaHas(uuid);
            if (!_user) {
                choppingMap.delete(uuid);
                logger.debug('debug:_user')
                return;
            }
            if (_user.get_battle()) {
                choppingMap.delete(uuid);
                logger.debug('debug:get_battle')
                return;
            }
            let __cell = _user.getPosition();

            if (__cell.x != element.start_pos.x || __cell.y != element.start_pos.y) {
                choppingMap.delete(uuid);
                logger.debug('debug:start_pos')
                return;
            }
            element.pgs -= 1;
            if (element.pgs <= 0) {
                let items = [app.createCfgItem('prop-20', 0, '砍树获得')]
                items[0].cont_change(app.generateRandom(1, _user.leve_chopping.num))
                let list = _user.addItems(items)
                let exp = app.random(1,_user.leve_chopping.num)
                _user.addExp_chopping(exp)
                list.push({ name: `🥳你已砍倒${element.pos.name}树木,障碍物已清除`, num: 1 })
                list.push({ name: `⏳伐木经验`, num: exp })
                let tpl: Tpl_gifts_default = {
                    title: `开荒完成`,
                    items: list
                }
                manage_task.event(taskKey.砍树次, 1, _user)
                _user.log.add(rank_type.砍树次数, 1)
                _user.sendMsg(template.通用奖励, tpl)
                choppingMap.delete(uuid);
                let __word = __cell.getWord();
                if (!__word.cellGroup.has(`${element.pos.x}_${element.pos.y}`)) {
                    __word.cellGroup.set(`${element.pos.x}_${element.pos.y}`, undefined as any)
                }
                return;
            }
            let one = await god.oneMore()
            _user.sendMsg(template.default_none, `「${one.str}」\n🪓努力砍树中...😰预计还需砍${element.pgs}下`);
        });
    }, 3000)
}